import com.lishanyang.hibernate.entity.UserEntity;
import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class TestUser00 {
    public static void main(String[] args) {
//        new TestGetUser().addUser();
//        new TestGetUser().getUserById(9);
//        new TestGetUser().deleteUserById(9);
//        new TestGetUser().deleteUserById(10);
//        new TestGetUser().deleteUserById(11);
//        new TestGetUser().updatebyId(13);
        TestUser testUser=new TestUser();
        testUser.testAdd();
    }

    private void addUser() {
        UserEntity userEntity = new UserEntity();
//        userEntity.setId();
        userEntity.setName("李羊羊");
        userEntity.setAge(18);
        userEntity.setAddr("杭州市余杭区中泰街道星未来云邸17-2-601");
        //Configuration对象对应于Hibernate的基本配置信息和对象关系映射文件
        Configuration configuration = new Configuration().configure();
        //创建一个SessionFactory对象
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //创建一个Session对象
        Session session = sessionFactory.openSession();
        Transaction transaction = null;

        try {
            //开启事务
            transaction = session.beginTransaction();
            session.save(userEntity);
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
            //自循环
            this.addUser();
        } finally {
            sessionFactory.close();
        }

    }

    private void getUserById(int i) {
        //Configuration对象对应于Hibernate的基本配置信息和对象关系映射文件
        Configuration configuration = new Configuration().configure();
        //创建一个SessionFactory对象
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //创建一个Session对象
        Session session = sessionFactory.openSession();

        UserEntity userEntity = (UserEntity) session.get(UserEntity.class, i);

        System.out.println(userEntity.getName() + "住在" + userEntity.getAddr() + "。");

        sessionFactory.close();
    }

    private void deleteUserById(int i) {
        //创建configuration
        Configuration configuration = new Configuration().configure();
        //创建一个SessionFactory对象
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //SessionFactory打开一个session
        Session session = sessionFactory.openSession();

        Transaction transaction = null;

        //查找要删除的数据
        UserEntity userEntity = (UserEntity) session.get(UserEntity.class, i);
        try {
            transaction = session.beginTransaction();
            session.delete(userEntity);
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        } finally {
            sessionFactory.close();
        }
    }

    private void updatebyId(int i) {
        //创建configuration
        Configuration configuration = new Configuration().configure();
        //创建一个SessionFactory对象
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //SessionFactory打开一个session
        Session session = sessionFactory.openSession();

        Transaction transaction = null;

        UserEntity userEntity = (UserEntity) session.get(UserEntity.class, i);
        if (userEntity != null) {
            userEntity.setName("李沐阳");
            try {
                transaction = session.beginTransaction();
                session.update(userEntity);
                transaction.commit();
            } catch (Exception e) {
                if (transaction != null) {
                    transaction.rollback();
                }
                e.printStackTrace();
            } finally {
                sessionFactory.close();
            }
        }


    }
}
